//
//  PublishImageViewCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/7.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol PublishImageViewCellDelegate: NSObjectProtocol {
    func imageCell(didClickedPickerImage cell: PublishImageViewCell)
    func imageCell(_ cell: PublishImageViewCell, didDeleteImage vm: PublishViewModel)
}

class PublishImageViewCell: UITableViewCell {
    
    private var viewModel = PublishViewModel() {
        didSet {
            collectionView.reloadData()
        }
    }
    
    weak var delegate: PublishImageViewCellDelegate?
    
    func configure(with vm: PublishViewModel, delegate: PublishImageViewCellDelegate?) {
        self.delegate = delegate
        self.viewModel = vm
    }
    
    @IBOutlet weak var collectionView: UICollectionView! {
        didSet {
            collectionView.delaysContentTouches = false
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.register(PublishPhotoCell.nibForCell(),
                                    forCellWithReuseIdentifier: PublishPhotoCell.reuseIdentifier())
        }
    }
}

extension PublishImageViewCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if !viewModel.deleteEnabled {
            return viewModel.numberOfItems
        }
        return viewModel.numberOfItems >= 9 ? 9 : viewModel.numberOfItems + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PublishPhotoCell.reuseIdentifier(),
                                                      for: indexPath) as! PublishPhotoCell
        cell.configure(with: viewModel, indexPath: indexPath)
        cell.delegate = self
        
        return cell
    }
}

extension PublishImageViewCell: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let itemWH = (screenW - 15 * 4) / 3
        return CGSize(width: itemWH, height: itemWH)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        if indexPath.item == viewModel.numberOfItems {
            delegate?.imageCell(didClickedPickerImage: self)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? PublishPhotoCell
        cell?.backgroundColor = .splLineNormal
    }
    
    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? PublishPhotoCell
        cell?.backgroundColor = .clear
    }
}

// MARK: - PublishPhotoCellDelegate
extension PublishImageViewCell: PublishPhotoCellDelegate {
    func deleteImage(_ cell: PublishPhotoCell) {
        if !viewModel.deleteEnabled {
            return
        }
        guard let indexPath = collectionView.indexPath(for: cell) else { return }
        viewModel.remove(at: indexPath)
        collectionView.reloadData()
        delegate?.imageCell(self, didDeleteImage: viewModel)
    }
}
